import requests
import hashlib
import json

# ----------------------登录----------------------
username = 'ammyxh01'
password = 'ammyxh01'
md5 = hashlib.md5(password.encode())  # 创建MD5对象
password_md5 = md5.hexdigest()  # 调用方法hexdigest()加密
# 构造请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
    'Content-Type': 'application/json;charset=UTF-8'
}
# 构造 url
url_base = r'https://cloud.huaxiaerp.com'
url_login = url_base + r'/jshERP-boot/user/login'
# 构造 data
data_login = {"loginName": username, "password": password_md5}
# 发送请求
resp = requests.post(url_login, json=data_login, headers=headers, verify=False)
# 获取响应
token = resp.json()['data']['token']
print(token)

# # ----------------------角色管理：新增----------------------
# name = 'jiaose01'
# type = '全部数据'
# # 构造请求头
# headers['X-Access-Token'] = token
# # 构造 url
# url_role_add = url_base + r'/jshERP-boot/role/add'
# # 构造 data
# data_role_add = {"name": name, "type": type}
# # 发送请求
# resp = requests.post(url_role_add, json=data_role_add, headers=headers, verify=False)
# # 获取响应
# print(resp.text)

# # ----------------------角色管理：修改----------------------
# id = 10253
# name = 'jiaose02'
# type = '全部数据'
# # 构造请求头
# headers['X-Access-Token'] = token
# # 构造 url
# url_role_mod = url_base + r'/jshERP-boot/role/update'
# # 构造 data
# data_role_mod = {"deleteFlag": "0", "enabled": True, "id": id, "name": name, "tenantId": 150490, "type": type}
# # 发送请求
# resp = requests.put(url_role_mod, json=data_role_mod, headers=headers, verify=False)
# # 获取响应
# print(resp.text)

# # ----------------------角色管理：查询----------------------
name = 'jiaose02'
description = ''
# 构造请求头
headers['X-Access-Token'] = token
# 构造 url
url_role_query = url_base + r'/jshERP-boot/role/list'
# 构造 data
search_dict = {"name": name, "description": description}  # 把内层的字典单独定义
search_str = json.dumps(search_dict)  # 把内层字典转成串
data_role_query = {
    'search': search_str,
    'column': 'createTime',
    'order': 'desc',
    'field': ['id', '', '', 'action', 'name', 'type', 'priceLimitStr', 'description', 'sort', 'enabled'],
    'currentPage': 1,
    'pageSize': 10
}

# 发送请求
resp = requests.get(url_role_query, params=data_role_query, headers=headers, verify=False)
# 获取响应
print(resp.text)

# ----------------------角色管理：删除----------------------
id = 10253
# 构造请求头
headers['X-Access-Token'] = token
# 构造 url
url_role_del = url_base + r'/jshERP-boot/role/delete'
# 构造 data
data_role_del = {"id": id}
# 发送请求
resp = requests.delete(url_role_del, params=data_role_del, headers=headers, verify=False)
# 获取响应
print(resp.text)
